package com.example.wupic.bestball;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class LibActivity extends AppCompatActivity {
    String t_lib_id="";
    String t_lib_total="";
    TextView text_lib_id;
    RequestQueue queue=MyApplication.requestQueue;
    NetworkImageView s_lib_icon;
    TextView s_lib_name;
    TextView s_lib_total_num;
    TextView s_lib_repeat_num;
    TextView s_lib_not_num;
    Button  s_btn_random;
    Button  s_btn_sequence;
    String recordflag="";//背题记录标志位
    JSONArray records=null;//背题记录
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lib);
        Intent intent= getIntent();
        String lib_id= intent.getStringExtra("lib_id");
        findControl();//找到控件
//         text_lib_id= findViewById(R.id.lib_id);
//         text_lib_id.setText(lib_id);//显示题库id
          getData(lib_id);

          s_btn_sequence.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //点击方法
                  //插入背题记录 并且打开背题界面
                 if(recordflag.equals("false")){
                     insertRecord();
                 }else{
                      try {
                          Intent intent=new Intent(LibActivity.this,QuestionActivity.class);

                          JSONObject record= (JSONObject) records.get(0);
                          String current=  record.get("t_lib_current").toString();
                          intent.putExtra("current_question_id",current);//把当前的背题记录传给下一个activity
                          startActivity(intent);
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }


                 }

              }
          });
    }
    public void findControl(){
        s_lib_icon=  findViewById(R.id.s_lib_icon);
        s_lib_name=  findViewById(R.id.s_lib_name);
        s_lib_total_num=  findViewById(R.id.s_lib_total_num);
        s_lib_repeat_num=  findViewById(R.id.s_lib_repeat_num);
        s_lib_not_num=  findViewById(R.id.s_lib_not_num);
        s_btn_random=  findViewById(R.id.s_btn_random);
        s_btn_sequence=  findViewById(R.id.s_btn_sequence);

    }
    public void insertRecord(){
        //调用volley 插入数据
        //取出登录用户数据
        JSONObject user= (JSONObject) MyApplication.session.get("user");
        try {
            String user_id= user.get("t_user_id").toString();
            String url="http://www.wupicheng.com:7888/study/newrecord?" +
                    "t_user_id="+user_id+"&t_lib_id="+t_lib_id+
                     "&t_lib_total="+t_lib_total;
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                       //数据插入成功之后 打开背题界面
                    try {
                       String success= jsonObject.get("success").toString();
                       if(success.equals("true")){
                           //打开界面
                           Intent intent=new Intent(LibActivity.this,QuestionActivity.class);
                           intent.putExtra("current_question_id","1");
                           startActivity(intent);
                       }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            MyApplication.requestQueue.add(jsonObjectRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public void getData(String lib_id){
        JSONObject user= (JSONObject) MyApplication.session.get("user");
        String t_user_id= null;
        try {
            t_user_id = user.get("t_user_id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://www.wupicheng.com:7888/user/querylibbyid2?t_lib_id="+lib_id
                +"&t_user_id="+t_user_id;
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    String success=  jsonObject.get("success").toString();
                    if (success.equals("true")){
                     JSONArray jsonArray= (JSONArray) jsonObject.get("libs");
                     records= (JSONArray) jsonObject.get("records");//背题记录
                     recordflag= jsonObject.get("recordflag").toString();//背题记录标志位
                     JSONObject jsonObject1= (JSONObject) jsonArray.get(0);
                     //显示数据
                       MyApplication.session.put("lib",jsonObject1);//放到session中备用
                       t_lib_id= jsonObject1.get("t_lib_id").toString();
                       t_lib_total= jsonObject1.get("t_lib_total").toString();

                        s_lib_name.setText(jsonObject1.get("t_lib_name").toString());
                        s_lib_total_num.setText(jsonObject1.get("t_lib_total").toString());
                        String imgurl=jsonObject1.get("t_lib_url").toString();
                        s_lib_icon.setDefaultImageResId(R.drawable.icon_default_lib);
                        s_lib_icon.setErrorImageResId(R.drawable.icon_default_lib);
                        s_lib_icon.setImageUrl(imgurl,MyApplication.imageLoader);
//                        Toast.makeText(LibActivity.this,
//                                jsonObject1.toString(),Toast.LENGTH_LONG).show();

                    }else{
                        Toast.makeText(LibActivity.this,
                                "网络访问失败",Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LibActivity.this,
                        volleyError.getLocalizedMessage(),Toast.LENGTH_LONG).show();
            }
        });
        queue.add(jsonObjectRequest);
    }
}
